<?php

declare(strict_types=1);

namespace DreamCat\ControllerParamGen\HelperClass\Container;

use Psr\Container\ContainerInterface;
use RuntimeException;

/**
 * 示例用的IoC容器
 * @author vijay
 */
class DemoContainer implements ContainerInterface
{
    private array $cache = [];

    public function __construct()
    {

    }

    /**
     * @inheritDoc
     */
    public function get(string $id)
    {
        if (array_key_exists($id, $this->cache)) {
            return $this->cache[$id];
        } else {
            throw new RuntimeException("{$id} 无法构建");
        }
    }

    /**
     * @inheritDoc
     */
    public function has(string $id): bool
    {
        return isset($this->cache[$id]);
    }

    public function add(string $id, mixed $obj): void
    {
        $this->cache[$id] = $obj;
    }
}

# end of file
